﻿using JyControl;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private List<Jyc> list = new List<Jyc>();

        private void Form1_Load(object sender, EventArgs e)
        {
            DirectoryInfo TheFolder = new DirectoryInfo("plug-in");

            //遍历文件
            foreach (FileInfo NextFile in TheFolder.GetFiles())
            {
                if (NextFile.Extension == ".dll")
                {
                    Assembly asm = Assembly.LoadFile(NextFile.FullName);
                    Type[] t = asm.GetExportedTypes();
                    foreach (Type type in t)
                    {
                        if (type.BaseType.GUID == typeof(Jyc).GUID)
                        {
                            Jyc uc = (Jyc)Activator.CreateInstance(type);
                            uc.Dock = DockStyle.Fill;
                            list.Add(uc);
                        }
                    }
                }
            }
            ShowList();
        }

        private void ShowList()
        {
            listBox1.Items.Clear();
            foreach (var item in list)
            {
                listBox1.Items.Add(item.Title);
            }
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.panel1.Controls.Clear();
            if (this.listBox1.SelectedIndex >= 0)
                this.panel1.Controls.Add(list[this.listBox1.SelectedIndex]);
        }
    }
}